require "test/unit"

require 'test_helper'

class TestAddRecords < Test::Unit::TestCase

  # Called before every test method runs. Can be used
  # to set up fixture information.
  def setup
    product = Product.new
    product.id = 1 # We set the ID ourselves here.
    product.description = "basic test, nothing special"
    product.price = 10
    @save = product.save
    assert(@save, "It failed")
  end

  # Called after every test method runs. Can be used to tear
  # down fixture information.

  def teardown
    #remove all db entries.

    all_records = Product.find(:all)
    all_records.each { |record| record.destroy }
    all_records = Product.find(:all)
    assert_equal(0, all_records.length)
  end

  #test to see if we can find something by hardcoded ID
  def test_db_find_by_id_hardcoded

    #id should be 1
    found_product = Product.find(1)
    assert_not_nil(found_product)
  end

  def test_db_find_by_first_and_all

    found_product = Product.find(:first)
    assert_not_nil(found_product);

    all_records = Product.find(:all)
    assert_equal(1, all_records.length)

  end

  #This adds stuff when sanitation is not necessary
  def test_db_find_with_conditions_unsafely_using_string

    id = 1
    description = "basic test, nothing special"

    #Uses sql statements in a string
    found_products = Product.find(:first, :conditions => "id = '#{id}' AND description = '#{description}'")
    assert_not_nil(found_products)

    #this should fail since we have nothing for the second record
    id = 2
    found_products = Product.find(:first, :conditions => "id = '#{id}' AND description = '#{description}'")
    assert_nil(found_products)

  end

  #Rails makes sure to now allow SQL injection this way.
  def test_db_find_with_conditions_safely_using_array
    id = 1
    description = "basic test, nothing special"

    found_products = Product.find(:first, :conditions => ["id = ? AND description = ?", id, description])
    assert_not_nil(found_products)
  end

  def test_db_find_with_condtions_safely_using_hash

    id = 1
    description = "basic test, nothing special"
    found_products = Product.find(:first, :conditions => {:id => id, :description  => description})

    assert_not_nil(found_products)
  end

  def test_db_find_with_conditions_using_range

    #found_products = Product.find(:all, :conditions => {:price => 9..12})
    #This doesn't seem supported by postgres
    #assert_not_nil(found_products)
  end

  def test_db_find_with_limit
    product = Product.new
    product.id = 2 # We set the ID ourselves here.
    product.description = "Second Row"
    product.price = 12
    @save = product.save

    product = Product.new
    product.id = 3 # We set the ID ourselves here.
    product.description = "Third Row"
    product.price = 14
    @save = product.save

    product = Product.find(:all, :limit => 2)
    assert_equal(2, product.length)

    product = Product.find(:all, :limit => 3)
    assert_equal(3, product.length)
  end

  def test_db_find_with_offset
    product = Product.new
    product.id = 2 # We set the ID ourselves here.
    product.description = "Second Row"
    product.price = 12
    @save = product.save

    product = Product.new
    product.id = 3 # We set the ID ourselves here.
    product.description = "Third Row"
    product.price = 14
    @save = product.save

    #For some reason you can't do offset without a limit. Not sure why. This seems like a Rails problem.
    #Though I'm guessing it's not.
    product = Product.find(:all, :offset => 2, :limit => 1)
    assert_equal(1,product.length )
  end

end




